import { reactive, toRefs } from "vue";
import Taro from "@tarojs/taro";
import http from "@/utils/http";

interface User {
  nickName: string;
  avatarUrl: string;
  gender: number;
}

interface Info {
  balanceA: number;
  balanceB: number;
  deduction_balance: number;
  deduction_freeze: number;
  id: number;
  name: string;
  phone: any;
  referralCode: number;
}

interface Business {
  balanceA: "string";
  balanceB: "string";
  address: "string";
  phone: "string";
  latitude: "string";
  name: "string";
  id: "string";
  contacts: "string";
  longitude: "string";
}

class Store {
  // 使用reactive替代多个ref
  private state = reactive({
    user: {
      nickName: "",
      avatarUrl: "",
      gender: 0,
    },
    info: {
      balanceA: 0,
      balanceB: 0,
      deduction_balance: 0,
      deduction_freeze: 0,
      id: 0,
      name: "",
      phone: null,
      referralCode: 817197,
      avatar_url: "",
    },
    business: {
      balanceA: "",
      balanceB: "",
      address: "",
      phone: "",
      latitude: "",
      name: "",
      id: "",
      contacts: "",
      longitude: "",
    },
    token: Taro.getStorageSync("token") || "",
    userType: Taro.getStorageSync("userType") || "",
    num: 0,
  });

  // 导出响应式状态
  public storeState = toRefs(this.state);

  constructor() {
    this.state.token = Taro.getStorageSync("token") || "";
    if (this.state.token) {
      console.warn("000000")
      this.getUserInfo();
    }
    this.state.user = Taro.getStorageSync("user") || {};
  }

  setUser(user: User) {
    this.state.user = user;
    console.log("this.user", this.state.user);
    Taro.setStorageSync("user", user);
  }

  setInfo(info: Info) {
    this.state.info = info;
    Taro.setStorageSync("info", info);
  }

  setUserType(type: string) {
    this.state.userType = type;
    Taro.setStorageSync("userType", type);
  }

  getUserName(){
    if(this.state.userType == "B"){
      return this.state.business.name
    }else{
      return this.state.info.name
    }
  }

  getUserInfo() {
    http.post("/api/user/getUser").then((res: any) => {
      console.log("token获取用户", res);
      if (res.data[0].columns) {
        // this.state.info = res.data[0].columns;
        this.setInfo(res.data[0].columns);
        console.log("token获取用户", this.state.info);
      }
    }).catch((err) => {
      console.log("token获取失败", err);
      Taro.switchTab({
        url: "/pages/my/index"
      })
    })
    // if (this.state.userType == "B") {
    //   http.post("/api/merchant/getUserById").then((res: any) => {
    //     console.log("token获取商家", res);
    //     if (res.data[0].columns) {
    //       this.state.business = res.data[0].columns;
    //       console.log("token获取商家", this.state.business);
    //     }
    //   }).catch((err) => {
    //     console.log("token获取商家失败", err);
    //     Taro.switchTab({
    //       url: "/pages/my/index"
    //     })
    //   })
    // } else {
    //   http.post("/api/user/getUser").then((res: any) => {
    //     console.log("token获取用户", res);
    //     if (res.data[0].columns) {
    //       // this.state.info = res.data[0].columns;
    //       this.setInfo(res.data[0].columns);
    //       console.log("token获取用户", this.state.info);
    //     }
    //   }).catch((err) => {
    //     console.log("token获取商家失败", err);
    //     Taro.switchTab({
    //       url: "/pages/my/index"
    //     })
    //   })
    // }
  }

  setToken(token: string) {
    this.state.token = token;
    Taro.setStorageSync("token", token);
  }
}

export const store = new Store();
